﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Windows;
using System.Collections.ObjectModel;
using action = System.Windows.Interactivity;
using System.Collections;

namespace XWord.WPF.Behaviors
{
    public class TriggerList : List<action.TriggerBase>
    {
    }


    public class InteractionHost
    {
        public static TriggerList GetTriggers(DependencyObject obj)
        {
            TriggerList triggers = (TriggerList)obj.GetValue(TriggersProperty);
            if (triggers == null)
            {
                triggers = new TriggerList();
                obj.SetValue(TriggersProperty, triggers);
            }
            return triggers;
        }

        public static readonly DependencyProperty TriggersProperty =
            DependencyProperty.RegisterAttached("Triggers", typeof(TriggerList), typeof(InteractionHost),
            new UIPropertyMetadata(new PropertyChangedCallback((obj, arg) =>
            {
                FrameworkElement element = obj as FrameworkElement;
                if (arg.NewValue != null)
                {
                    var list = arg.NewValue as IList;
                    foreach (var item in list)
                    {
                        action.Interaction.GetTriggers(obj).Add(Clone(item as PropertyTrigger));
                    }
                }
            })));

        public static PropertyTrigger Clone(PropertyTrigger trigger)
        {
            PropertyTrigger newObj = new PropertyTrigger();
            newObj.PropertyName = trigger.PropertyName;
            newObj.Value = trigger.Value;
            foreach (DpSetterBase item in trigger.Actions)
            {
                var visualSetter = item as VisualSetter;
                if (visualSetter != null)
                {
                    var newAction = new VisualSetter();
                    newAction.Property = item.Property;
                    newAction.Value = item.Value;
                    newAction.Mode = item.Mode;
                    newAction.TargetName = visualSetter.TargetName;
                    newAction.TargetType = visualSetter.TargetType;
                    newObj.Actions.Add(newAction);
                }
                else
                {
                    var newAction = new DpSetter();
                    newAction.Property = item.Property;
                    newAction.Value = item.Value;
                    newAction.Mode = item.Mode;
                    newObj.Actions.Add(newAction);
                }
            }
            return newObj;
        }
    }
}
